package com.us.util;

import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.BooleanUtils;

/**
 * 对Bean的一些操作<br>
 * 可参考 <code>http://commons.apache.org/beanutils/</code> <br>
 * 扩展类
 * 
 * @author monlyu
 */
public class BeanUtil {

    /**
     * 设置属性
     * 
     * @since 2009-12-28
     */
    public static void setProperty(Object bean, String name, Object value) {
        if (StringUtil.isNotEmpty(value)) {
            try {
                Class<?> clazz = ClassUtil.getFiledType(bean, name);
                if (clazz != null) {
                    // if (ClassUtil.isInDefVoPackage(clazz)) {}
                    if (RegexUtil.isDate(value.toString())) {
                        if (clazz.equals(Date.class) || clazz.equals(java.sql.Date.class)) {
                            BeanUtils.setProperty(bean, name, DateUtil.parse(value.toString(), DateUtil.DATE_PATTERN));
                            return;
                        }
                    }
                    if (RegexUtil.isDateTime(value.toString())) {
                        if (clazz.equals(Date.class) || clazz.equals(java.sql.Date.class)) {
                            BeanUtils.setProperty(bean, name, DateUtil.parse(value.toString(), DateUtil.DATETIME_PATTERN));
                            return;
                        }
                    }
                    if (RegexUtil.isBol(value.toString())) {
                        BeanUtils.setProperty(bean, name, BooleanUtils.toBoolean(value.toString()));
                        return;
                    }
                    BeanUtils.setProperty(bean, name, value);
                }
            } catch (Exception e) {
                UsLogUtil.getLog(BeanUtil.class).error("设置属性异常:" + e);
            }
        }
    }
}
